package com.xuwang.service.impl;

import com.xuwang.Util.JWTutil;
import com.xuwang.mapper.AdditiveMapper;
import com.xuwang.mapper.ProductionMapper;
import com.xuwang.pojo.FoodSample;
import com.xuwang.mapper.FoodSampleMapper;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 菜品留样表(FoodSample)表服务实现类
 *
 * @author abaaba
 * @since 2021-09-20 12:42:05
 * @version 1.0
 */
@Service("foodSampleService")
public class FoodSampleServiceImpl {
    @Resource
    private FoodSampleMapper foodSampleMapper;

    @Resource
    private AdditiveMapper additiveMapper;
    @Resource
    private ProductionMapper productionMapper;





    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        String batchNo = foodSampleMapper.selectBatchNoById(id);
        Integer type = productionMapper.selectMatchTypeByBatchNo(batchNo);

        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.foodSampleMapper.selectById(id));
        map.put("food", this.productionMapper.selectByBatchNo(batchNo,type));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param index 查询起始位置
     * @return 对象列表
     */
    public Map<String, Object> selectForPage(int m ,int index, int page,
                                             String foodBatchNo,String foodName,String companyName,HttpServletRequest request) {
        // 获取当前表中的总记录
        if(m==2){
            String token = request.getHeader("Authorization");
            String userName = JWTutil.getUserName(token);

//        String userName="销售人";
        companyName=additiveMapper.selectCompanyNameByUsername(userName);
        }
    // 获取当前表中的总记录
        int tableCount = this.foodSampleMapper.selectForCount(foodBatchNo,foodName,companyName);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / page + 1;
        // 计算每页开始的下标值
        index = (index - 1) * page;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.foodSampleMapper.selectForPage(index, page,foodBatchNo,foodName,companyName));
        return map;
    }

    /**
     * 新增数据
     *
     * @param foodSample 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(FoodSample foodSample, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userName = JWTutil.getUserName(token);
        Map<String, Object> map = new HashMap<>();
//        String userName="销售人";
        foodSample.setCompanyName(additiveMapper.selectCompanyNameByUsername(userName));
        Integer integer = foodSampleMapper.selectIdByFoodBatchNoAndCompany(foodSample.getFoodBatchNo(), foodSample.getCompanyName());
        if (integer!=null){
            this.foodSampleMapper.updateById(foodSample);
            map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
            map.put("msg", "更新成功");
        }else {
            // UUID.randomUUID()  返回内容：asd21321-ewrewrew213213-123213zsad-123asdasd这样的形态
            this.foodSampleMapper.insert(foodSample);
            map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
            map.put("msg", "新增成功");
        }
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param foodSample 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(FoodSample foodSample) {
        this.foodSampleMapper.updateById(foodSample);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "更新成功");
        return map;
    }


}